﻿using System.Collections.Generic;
using System.Linq;
using IceDog.DNF.Program.WebApi2.Models;

namespace IceDog.DNF.Program.WebApi2.Repository
{
    /// <summary>
    /// 产品仓库默认实现
    /// </summary>
    public class ProductRepository : IProductRepository
    {
        private List<Product> _products;
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ProductRepository()
        {
            //初始化模拟数据
            _products = new List<Product>
            {
                new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1M },
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
                new Product { Id = 3, Name = "Teddy bear", Category = "Toys", Price = 3.75M },
                new Product { Id = 4, Name = "Hammer", Category = "Hardware", Price = 16.99M },
                new Product { Id = 5, Name = "Black Tea", Category = "Drinks", Price = 2.0M },
                new Product { Id = 6, Name = "Green Tea", Category = "Drinks", Price = 2.0M },
                new Product { Id = 7, Name = "Coca Cola", Category = "Drinks", Price = 2.5M },
                new Product { Id = 8, Name = "Apple", Category = "Fruits", Price = 5.99M },
                new Product { Id = 9, Name = "Banana", Category = "Fruits", Price = 4.9M },
                new Product { Id = 10, Name = "Orange", Category = "Fruits", Price = 5M },
                new Product { Id = 11, Name = "Watermelon", Category = "Fruits", Price = 1.5M },
                new Product { Id = 12, Name = "Peach", Category = "Fruits", Price = 3.89M },
            }; ;
        }
        /// <summary>
        /// 获取所有产品
        /// </summary>
        /// <returns>返回产品列表</returns>
        public IEnumerable<Product> GetAllProducts()
        {
            return _products;
        }

        /// <summary>
        /// 根据id获取产品
        /// </summary>
        /// <param name="id">产品编号</param>
        /// <returns>返回对应id的产品</returns>
        public Product GetProductById(int id)
        {
            return _products.FirstOrDefault(p => p.Id.Equals(id));
        }

        /// <summary>
        /// 根据分类获取产品
        /// </summary>
        /// <param name="category">产品分类</param>
        /// <returns>返回对应id的产品</returns>
        public IEnumerable<Product> GetProductByCategory(string category)
        {
            return _products.Where(p => p.Category.Equals(category));
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="product">产品对象</param>
        /// <returns>添加的产品</returns>
        public Product AddProduct(Product product)
        {
            var maxId = _products.Select(p=>p.Id).Max();
            product.Id = maxId + 1;
            _products.Add(product);
            return product;
        }

        /// <summary>
        /// 移除产品
        /// </summary>
        /// <param name="id">产品编号</param>
        /// <returns>是否移除成功</returns>
        public bool RemoveProduct(int id)
        {
            var product = _products.Find(p => p.Id.Equals(id));
            if (product == null)
            {
                return false;
            }
            _products.Remove(product);
            return true;
        }

        /// <summary>
        /// 更新产品
        /// </summary>
        /// <param name="id">产品编号</param>
        /// <param name="product">产品对象</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateProduct(int id, Product product)
        {
            var product1 = _products.Find(p => p.Id.Equals(id));
            if (product1 == null)
            {
                return false;
            }
            product1.Name = product.Name;
            product1.Category = product.Category;
            product1.Price = product.Price;
            return true;
        }
    }
}